//
//  DownloadedBookCell.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-12.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "DownloadedBookCell.h"
#import "UIImageView+WebCache.h"
#import <QuartzCore/QuartzCore.h>

@implementation DownloadedBookCell

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        
        coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(7, 11, 83, 105)];
        coverImageView.contentMode = UIViewContentModeScaleAspectFit;
        coverImageView.image = [UIImage imageNamed:@"default_image"];
        [self.contentView addSubview:coverImageView];
        
        coverImageView.layer.shadowColor = [UIColor blackColor].CGColor;
        coverImageView.layer.shadowOpacity = 0.45f;
        coverImageView.layer.shadowOffset = CGSizeMake(0, 3);
        coverImageView.layer.masksToBounds = NO;
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:coverImageView.bounds];
        coverImageView.layer.shadowPath = path.CGPath;
        
        checkImageView = [[UIImageView alloc] initWithFrame:CGRectMake(66, 12, 21, 21)];
        checkImageView.hidden = YES;
        [self.contentView addSubview:checkImageView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (selected) {
        if (self.editing) {
            if (multiSelected) {
                checkImageView.image = [UIImage imageNamed:@"checkbox"];
                [_booksToDeleteArray removeObject:_item_id];
            }
            else {
                checkImageView.image = [UIImage imageNamed:@"checkbox_selected"];
                [_booksToDeleteArray addObject:_item_id];
            }
            multiSelected = !multiSelected;
        }
    }
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    if (editing) {
        checkImageView.hidden = NO;
        if ([_booksToDeleteArray containsObject:_item_id]) {
            checkImageView.image = [UIImage imageNamed:@"checkbox_selected"];
            multiSelected = YES;
        }
        else {
            checkImageView.image = [UIImage imageNamed:@"checkbox"];
            multiSelected = NO;
        }
    }
    else {
        checkImageView.hidden = YES;
        multiSelected = NO;
    }
}

- (void)updateDisplay
{
    NSString *imageUrl = [[[[[NSUserDefaults standardUserDefaults] objectForKey:kCoverImage] valueForKey:self.item_id] objectForKey:@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [coverImageView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"default_image"]];
}

- (void)prepareForReuse
{
    multiSelected = NO;
}

- (void)dealloc
{
    [_item_id release];
    [coverImageView release];
    [checkImageView release];
    
    [super dealloc];
}

@end
